package com.yanqu.road.dao.impl.activity.feast;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.feast.config.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FeastConfigDaoImpl extends TempDao {

    private static String getFeastPvpPlaceConfigSql = "select * from `t_s_activity_feast_pvp_place` where `activityId`=?";

    public Map<Integer, FeastPvpPlaceConfig> getFeastPvpPlaceConfigMap(int activityId) {
        Map<Integer, FeastPvpPlaceConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFeastPvpPlaceConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FeastPvpPlaceConfig feastPvpPlaceConfig = new FeastPvpPlaceConfig();
                feastPvpPlaceConfig.setActivityId(rs.getInt("activityId"));
                feastPvpPlaceConfig.setId(rs.getInt("id"));
//                feastPvpPlaceConfig.setType(rs.getInt("type"));
//                feastPvpPlaceConfig.setName(rs.getString("name"));
//                feastPvpPlaceConfig.setLevel(rs.getInt("level"));
//                feastPvpPlaceConfig.setRadius(rs.getInt("radius"));
//                feastPvpPlaceConfig.setSeizeScore(rs.getInt("seizeScore"));
//                feastPvpPlaceConfig.setSeizeProduce(rs.getInt("seizeProduce"));
                
                feastPvpPlaceConfig.setMapParam(rs.getString("mapParam"));
                feastPvpPlaceConfig.setUpDemand(rs.getInt("upDemand"));
                feastPvpPlaceConfig.setDownDemand(rs.getInt("downDemand"));
                feastPvpPlaceConfig.setSkillScore(rs.getInt("skillScore"));
                feastPvpPlaceConfig.setTimeScore(rs.getInt("timeScore"));
                feastPvpPlaceConfig.setFirstUpScore(rs.getInt("firstUpScore"));
                feastPvpPlaceConfig.setBackgroundSource(rs.getString("backgroundSource"));

                feastPvpPlaceConfig.setMapSizeList(StringUtils.stringToIntegerList(feastPvpPlaceConfig.getMapParam(), "\\|"));
                map.put(feastPvpPlaceConfig.getId(), feastPvpPlaceConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getFeastTripodConfigSql = "select * from `t_s_activity_feast_tripod` where `activityId`=?";
    public Map<Integer, FeastTripodConfig> getFeastTripodConfigMap(int activityId) {
        Map<Integer, FeastTripodConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFeastTripodConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FeastTripodConfig feastTripodConfig = new FeastTripodConfig();
                feastTripodConfig.setActivityId(rs.getInt("activityId"));
                feastTripodConfig.setLevel(rs.getInt("level"));
                feastTripodConfig.setNeedItem(rs.getString("needItem"));
                feastTripodConfig.setStateLimit(rs.getInt("stateLimit"));
                feastTripodConfig.setExtractConsume(rs.getInt("extractConsume"));
                feastTripodConfig.setQualityWeight(rs.getString("qualityWeight"));
                feastTripodConfig.setTypeRandom(rs.getString("typeRandom"));
                feastTripodConfig.setLevelRandom(rs.getString("levelRandom"));
                feastTripodConfig.setAdvancedLevelRandom(rs.getString("advancedLevelRandom"));
                feastTripodConfig.setSkillLevelRandom(rs.getString("skillLevelRandom"));
                map.put(feastTripodConfig.getLevel(), feastTripodConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getFeastEquipmentConfigSql = "select * from `t_s_activity_feast_equipment` where `activityId`=?";
    public Map<Integer, FeastEquipmentConfig> getFeastEquipmentConfigMap(int activityId) {
        Map<Integer, FeastEquipmentConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFeastEquipmentConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FeastEquipmentConfig feastEquipmentConfig = new FeastEquipmentConfig();
                feastEquipmentConfig.setActivityId(rs.getInt("activityId"));
                feastEquipmentConfig.setId(rs.getInt("id"));
                feastEquipmentConfig.setIcon(rs.getInt("icon"));
                feastEquipmentConfig.setType(rs.getInt("type"));
                feastEquipmentConfig.setQuality(rs.getInt("quality"));
                feastEquipmentConfig.setName(rs.getString("name"));
                feastEquipmentConfig.setDesc(rs.getString("desc"));
                feastEquipmentConfig.setAttribute(rs.getString("attribute"));
                feastEquipmentConfig.setAdvancedAttribute(rs.getString("advancedAttribute"));
                feastEquipmentConfig.setSkillType(rs.getInt("skillType"));
                feastEquipmentConfig.setSkillParameter(rs.getString("skillParameter"));
                feastEquipmentConfig.setRecoveryReward(rs.getString("recoveryReward"));
                map.put(feastEquipmentConfig.getId(), feastEquipmentConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getFeastNpcConfigSql = "select * from `t_s_activity_feast_npc` where `activityId`=?";
    public Map<Integer, FeastNpcConfig> getFeastNpcConfigMap(int activityId) {
        Map<Integer, FeastNpcConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFeastNpcConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FeastNpcConfig feastNpcConfig = new FeastNpcConfig();
                feastNpcConfig.setActivityId(rs.getInt("activityId"));
                feastNpcConfig.setId(rs.getInt("id"));
                feastNpcConfig.setIcon(rs.getInt("icon"));
                feastNpcConfig.setType(rs.getInt("type"));
                feastNpcConfig.setSmallType(rs.getInt("smallType"));
                feastNpcConfig.setAttributeType(rs.getInt("attributeType"));
                feastNpcConfig.setName(rs.getString("name"));
                feastNpcConfig.setDesc(rs.getString("desc"));
                feastNpcConfig.setAttribute(rs.getString("attribute"));
                feastNpcConfig.setAdvancedAttribute(rs.getString("advancedAttribute"));

                feastNpcConfig.setAttackType(rs.getInt("attackType"));
                feastNpcConfig.setStartParam(rs.getString("startParam"));
                feastNpcConfig.setEndParam(rs.getString("endParam"));
                feastNpcConfig.setVfxTime(rs.getInt("vfxTime"));
                feastNpcConfig.setAttackVfx(rs.getString("attackVfx"));
                feastNpcConfig.setBodyHeight(rs.getInt("bodyHeight"));


                map.put(feastNpcConfig.getId(), feastNpcConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getFeastEventConfigSql = "select * from `t_s_activity_feast_event` where `activityId`=?";
    public Map<Integer, FeastEventConfig> getFeastEventConfigMap(int activityId) {
        Map<Integer, FeastEventConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFeastEventConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FeastEventConfig feastEventConfig = new FeastEventConfig();
                feastEventConfig.setActivityId(rs.getInt("activityId"));
                feastEventConfig.setId(rs.getInt("id"));
                feastEventConfig.setType(rs.getInt("type"));
                feastEventConfig.setEventParameter(rs.getString("eventParameter"));
                feastEventConfig.setName(rs.getString("name"));
                feastEventConfig.setIntroduce(rs.getString("introduce"));
                feastEventConfig.setDesc(rs.getString("desc"));
                feastEventConfig.setRewards(rs.getString("rewards"));
                feastEventConfig.setFailRewards(rs.getString("failRewards"));
                feastEventConfig.setIcon(rs.getInt("icon"));
                feastEventConfig.setXianLi(rs.getLong("xianLi"));
                map.put(feastEventConfig.getId(), feastEventConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getFeastMissionConfigSql = "select * from `t_s_activity_feast_mission` where `activityId`=?";
    public Map<Integer, FeastMissionConfig> getFeastMissionMap(int activityId) {
        Map<Integer, FeastMissionConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFeastMissionConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FeastMissionConfig feastMissionConfig = new FeastMissionConfig();
                feastMissionConfig.setActivityId(rs.getInt("activityId"));
                feastMissionConfig.setId(rs.getInt("id"));
                feastMissionConfig.setType(rs.getInt("type"));
                feastMissionConfig.setFloor(rs.getInt("floor"));
                feastMissionConfig.setIcon(rs.getInt("icon"));
                feastMissionConfig.setName(rs.getString("name"));
                feastMissionConfig.setDesc(rs.getString("desc"));
                feastMissionConfig.setStateLimit(rs.getInt("stateLimit"));
                feastMissionConfig.setTerrain(rs.getString("terrain"));
                feastMissionConfig.setConnectDot(rs.getString("connectDot"));
                List<Integer> eventIdList = new ArrayList<>();
                List<String> stringList = StringUtils.stringToStringList(feastMissionConfig.getTerrain(), "\\|");
                for (String s : stringList) {
                    List<String> string = StringUtils.stringToStringList(s, "=");
                    int eventId = Integer.parseInt(string.get(1));
                    if(eventId == 2000 || eventId == 1000){
                        continue;
                    }
                    if(eventIdList.contains(eventId)){
                        throw new RuntimeException(String.format("地图 %s 事件配置错误", feastMissionConfig.getId()));
                    }
                    eventIdList.add(eventId);
                }
                feastMissionConfig.setEventIdList(eventIdList);

                feastMissionConfig.setSubsectionRewards(rs.getString("subsectionRewards"));
                feastMissionConfig.setBuff(rs.getString("buff"));
                map.put(feastMissionConfig.getId(), feastMissionConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getFeastStateConfigSql = "select * from `t_s_activity_feast_state` where `activityId`=?";
    public Map<Integer, FeastStateConfig> getFeastStateConfigMap(int activityId) {
        Map<Integer, FeastStateConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFeastStateConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FeastStateConfig feastStateConfig = new FeastStateConfig();
                feastStateConfig.setActivityId(rs.getInt("activityId"));
                feastStateConfig.setId(rs.getInt("id"));
                feastStateConfig.setStateType(rs.getInt("stateType"));
                feastStateConfig.setClassId(rs.getInt("classId"));
                feastStateConfig.setMaxEffect(rs.getInt("maxEffect"));
                feastStateConfig.setName(rs.getString("name"));
                feastStateConfig.setNeedItem(rs.getString("needItem"));
                feastStateConfig.setConsumeItem(rs.getString("consumeItem"));
                feastStateConfig.setNeedXianLi(rs.getString("needXianLi"));
                feastStateConfig.setMinXianLi(rs.getString("minXianLi"));
                feastStateConfig.setPracticeSpeed(rs.getInt("practiceSpeed"));
                feastStateConfig.setAttributeAdd(rs.getInt("attributeAdd"));
                feastStateConfig.setPvpScore(rs.getInt("pvpScore"));
                feastStateConfig.setPvpSpeed(rs.getInt("pvpSpeed"));
                map.put(feastStateConfig.getId(), feastStateConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getFeastUpgradeConfigSql = "select * from `t_s_activity_feast_upgrade` where `activityId`=?";
    public Map<Integer, Map<Integer, FeastUpgradeConfig>> getFeastUpgradeConfigMap(int activityId) {
        Map<Integer, Map<Integer, FeastUpgradeConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFeastUpgradeConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FeastUpgradeConfig feastUpgradeConfig = new FeastUpgradeConfig();
                feastUpgradeConfig.setActivityId(rs.getInt("activityId"));
                feastUpgradeConfig.setId(rs.getInt("id"));
                feastUpgradeConfig.setType(rs.getInt("type"));
                feastUpgradeConfig.setLevel(rs.getInt("level"));
                feastUpgradeConfig.setUpgradeAtt(rs.getString("upgradeAtt"));
                if (!map.containsKey(feastUpgradeConfig.getType())) {
                    map.put(feastUpgradeConfig.getType(), new ConcurrentHashMap<>());
                }
                map.get(feastUpgradeConfig.getType()).put(feastUpgradeConfig.getLevel(), feastUpgradeConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
    
}
